﻿using System;
using System.Data;
using personalplaner.business.common;
using personalplaner.core.sql;
using personalplaner.business.enums;

namespace personalplaner.dataprovider.services
{
    public class OptionsDataService
	{
		#region Get Methods

		public double NumberValue(DatabaseSettings settings, enOptions typ, int wert)
        {
            return NumberValue(settings, typ.ToString(), wert);
        }

        public double NumberValue(DatabaseSettings settings, string typ, int wert)
        {
            var str = string.Format("select zahlwert from Options where typ = '{0}' and wert = {1}", typ, wert);
            var table = SimpleQuerry.ExecuteForDataTable(settings, str);

            if (table.Rows.Count <= 0)
                return 0;

            return Convert.ToDouble(table.Rows[0].Field<decimal>("zahlwert"));
        }


        public string StringValue(DatabaseSettings settings, enOptions typ, int wert)
        {
            return StringValue(settings, typ.ToString(), wert);
        }

        public string StringValue(DatabaseSettings settings, string typ, int wert)
        {
            var str = string.Format("select textwert from Options where typ = '{0}' and wert = {1}", typ, wert);
            var table = SimpleQuerry.ExecuteForDataTable(settings, str);
            if (table.Rows.Count <= 0)
                return string.Empty;

            return table.Rows[0].Field<string>("textwert");
        }


        /// <summary>
        /// Gets the amount of options asociated with the type
        /// </summary>
        /// <param name="typ"></param>
        /// <returns></returns>
        public int OptionsCount(DatabaseSettings settings, string typ)
        {
            var str = string.Format("select count(typ) from Options where typ = '{0}'", typ);
            var table = SimpleQuerry.ExecuteForDataTable(settings, str);

            if (table.Rows.Count <= 0)
                return 0;

            return table.Rows[0].Field<int>(0);
        }

        /// <summary>
        /// Gets the amount of options asociated with the type
        /// </summary>
        /// <param name="typ"></param>
        /// <returns></returns>
        public int OptionsCount(DatabaseSettings settings, enOptions typ)
        {
            return OptionsCount(settings, typ.ToString());
		}

		#endregion

		#region Set Methods
		
		public void SaveData(DatabaseSettings settings, enOptions typ, int wert, double zahlwert, string textwert = "")
		{
			SaveData(settings, typ.ToString(), wert, zahlwert, textwert);
		}

    	public void SaveData(DatabaseSettings settings, string typ, int wert, double zahlwert, string textwert = "")
		{
			var stp = new Querry(settings, "upd_options");

			stp.AddParameter("@typin", typ);
			stp.AddParameter("@wertin", wert);
			stp.AddParameter("@zahlwertin", zahlwert);
			stp.AddParameter("@textwert", textwert);

			stp.Execute();
		}

		#endregion
	}
}
